package com.chuantou.yuba.ui;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.chuantou.yuba.R;
import com.chuantou.yuba.util.ChatUtils;
import com.chuantou.yuba.util.StackManager;
import com.easemob.applib.utils.CommonUtils;
import com.easemob.chat.EMMessage;
import com.easemob.chat.EMMessage.Type;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EasyUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * 基础Activity类
 * 
 * @author Administrator
 * 
 */
public class BaseActivity extends FragmentActivity {
	private boolean mDestroyed = false;
	private static final int notifiId = 11;
	protected NotificationManager mNotificationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		StackManager.onActivityCreated(this);
	}

	protected boolean hasDestroyed() {
		return mDestroyed;
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		StackManager.setCurrentActivity(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		mDestroyed = true;
		StackManager.onActivityDestroied(this);
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if (StackManager.hasActivityInStack(MainActivity.class.getName())) {
			super.onBackPressed();
		} else {
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		}
	}

	/**
	 * 当应用在前台时，如果当前消息不是属于当前会话，在状态栏提示一下 如果不需要，注释掉即可
	 * 
	 * @param message
	 */
	protected void notifyNewMessage(EMMessage message) {
		// 如果是设置了不提醒只显示数目的群组(这个是app里保存这个数据的，demo里不做判断)
		// 以及设置了setShowNotificationInbackgroup:false(设为false后，后台时sdk也发送广播)
		if (!EasyUtils.isAppRunningForeground(this)) {
			return;
		}

		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(getApplicationInfo().icon).setWhen(System.currentTimeMillis())
				.setAutoCancel(true);

		String ticker = CommonUtils.getMessageDigest(message, this);
		String st = getResources().getString(R.string.expression);
		if (message.getType() == Type.TXT)
			ticker = ticker.replaceAll("\\[.{2,3}\\]", st);
		String from = null;
		try {
			from = ChatUtils.getChatNickname(message);
		} catch (EaseMobException e) {
			e.printStackTrace();
		}
		if (TextUtils.isEmpty(from)) {
			from = message.getFrom();
		}
		// 设置状态栏提示
		mBuilder.setTicker(from + ": " + ticker);

		// 必须设置pendingintent，否则在2.3的机器上会有bug
		Intent intent = new Intent(this, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, notifiId, intent, PendingIntent.FLAG_ONE_SHOT);
		mBuilder.setContentIntent(pendingIntent);

		Notification notification = mBuilder.build();
		mNotificationManager.notify(notifiId, notification);
		mNotificationManager.cancel(notifiId);
	}
}
